//
//  Ext+UIButton.swift
//  langligelang
//
//  Created by 秦－政 on 2016/10/22.
//  Copyright © 2016年 pete. All rights reserved.
//

import UIKit

extension UIButton {

    /// 创建 button 快捷方式
    ///
    /// - parameter setImgName:        图片名字
    /// - parameter backgroundImgName: 背景图片名字
    /// - parameter target:             target
    /// - parameter action:            action
    ///
    /// - returns:
    convenience init(setImgName: String, backgroundImgName: String,target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        // 设置image
        self.setImage(UIImage(named: setImgName),for: UIControlState.normal)
        self.setImage(UIImage(named: "\(setImgName)_highlighted"), for: UIControlState.highlighted)
        // 设置背景图
        self.setBackgroundImage(UIImage(named: backgroundImgName), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named: "\(backgroundImgName)_highlighted"), for: UIControlState.highlighted)
        self.sizeToFit()
    }

    /// 设置 button
    ///
    /// - parameter setHighlightedImgName:  图片
    /// - parameter target:                target
    /// - parameter action:                action
    ///
    /// - returns:
    convenience init(setHighlightedImgName: String? = nil, titile: String? = nil,target: Any?, action: Selector){
        
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        // 判断图片和标题是否为空
        if let img = setHighlightedImgName {
            // 设置图片
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        if let ti = titile {
            // 设置标题
            self.setTitle(ti, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(SLTHEMECOLOR, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: SLNormalFontSize)
        }
        
        self.sizeToFit()
    }
    convenience init(setBackgroundImgName: String, title:  String, fontSize: CGFloat, titleColor: UIColor,  target: Any?, action: Selector) {
        self.init()
        self.setBackgroundImage(UIImage(named:setBackgroundImgName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.sizeToFit()
    }
}
